<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <el-col :span="20"><div class="grid-content bg-purple" />
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4"><div class="grid-content bg-purple" />
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item>添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <el-tree :data="treedata" :props="defaultProps" default-expand-all class="departments-tree" @node-click="handleNodeClick">
              <template slot-scope="{data}">
                <el-row type="flex" justify="space-between" style="height: 50px; width:100%" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4"><div class="grid-content bg-purple" />
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="showDialog=true">添加子部门</el-dropdown-item>
                            <el-dropdown-item>编辑部门</el-dropdown-item>
                            <el-dropdown-item>删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                      <!-- 添加或编辑弹框 -->
                      <!-- 1.  [在遮罩上点击]可以通过`:close-on-click-modal='false'`来关闭这个功能-->
                      <!-- 2.  按下esc键。可以通过`:close-on-press-escape='false'`来关闭这个功能  -->
                      <el-dialog
                        title="添加或编辑"
                        :visible.sync="showDialog"
                        :close-on-click-modal="false"
                        :close-on-press-escape="false"
                      >
                        <!-- 添加子组件弹层 -->
                        <deptDialog />
                        <span slot="footer" class="dialog-footer">
                          <el-button @click="showDialog = false">取 消</el-button>
                          <el-button type="primary" @click="showDialog = false">确 定</el-button>
                        </span>
                      </el-dialog>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>

        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments } from '@/api/department'
import { tranListToTreeData } from '@/utils'
import deptDialog from './deptDialog.vue'
export default {
  name: 'Departments',
  components: {
    deptDialog
  },
  data() {
    return {
      activeName: 'first', // 被激活的 Tab 标签页
      treedata: [],
      defaultProps: {
        lable: 'name'
      },
      showDialog: false // 控制添加或编辑弹框展示
    }
  },
  created() {
    // 调用获取组织架构列表数据方法
    this.getDepartmentsFn()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    // 获取组织架构列表数据
    async getDepartmentsFn() {
      const res = await getDepartments()
      console.log(res)
      this.treedata = tranListToTreeData(res.data.depts, '')
    }
  }
}
</script>

<style lang="scss" scoped>

.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}</style>
